﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.IO;
using System.Text;

namespace Projky.InstallHelper {
    [RunInstaller(true)]
    public partial class SetupAddinFile : System.Configuration.Install.Installer {
        #region Installer

        public SetupAddinFile() {
            InitializeComponent();
        }

        protected override void OnAfterUninstall(IDictionary savedState) {
            RemoveDenaliAddinFile();
            RemoveHekatonAddinFile();
            base.OnAfterUninstall(savedState);
        }

        public override void Rollback(IDictionary savedState) {
            RemoveDenaliAddinFile();
            RemoveHekatonAddinFile();
            base.Rollback(savedState);
        }

        public override void Install(IDictionary stateSaver) {
            base.Install(stateSaver);
            SetupDenaliAddinFile();
            SetupHekatonAddinFile();
            CreateProjkyAddinFolder();
            CreateScriptFoldersTextFile();
        }

        #endregion

        void CreateScriptFoldersTextFile() {
            string filePath = Path.Combine(Path.GetDirectoryName(this.GetType().Assembly.Location), "scriptfolders.txt");
            if (File.Exists(filePath) == false) {
                File.WriteAllText(filePath, 
@"// put you script folder here,single folder single row. example as:
C:\Users\signal\Documents\ProjkyTwo", Encoding.UTF8);
            }
        }

        void CreateProjkyAddinFolder() {
            string projkyFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\ProjkyAddin";
            if (Directory.Exists(projkyFolder) == false) {
                Directory.CreateDirectory(projkyFolder);
            }

            for (int i = 1; i < 4; i++){
                string filePath = Path.Combine(projkyFolder, String.Format("{0}.sql", i));
                string content = String.Format("SELECT {0};", i);
			    if (File.Exists(filePath) == false) {
                    File.WriteAllText(filePath, content, Encoding.UTF8);
                }
			}
        }

        void SetupDenaliAddinFile() {
            string fileContent;
            string denaliFilePath;

            fileContent = @"<?xml version=""1.0"" encoding=""{0}"" standalone=""no""?>
                        <Extensibility xmlns=""http://schemas.microsoft.com/AutomationExtensibility"">
	                        <HostApplication>
		                        <Name>Microsoft SQL Server Management Studio</Name>
		                        <Version>*</Version>
	                        </HostApplication>
	                        <Addin>
		                        <FriendlyName>ProjkyAddin</FriendlyName>
		                        <Description>ProjkyAddin for sql 2012</Description>
		                        <Assembly>{1}\Projky.ForDenali.dll</Assembly>
		                        <FullClassName>Projky.ForDenali.Connect</FullClassName>
		                        <LoadBehavior>1</LoadBehavior>
		                        <CommandPreload>1</CommandPreload>
		                        <CommandLineSafe>0</CommandLineSafe>
	                        </Addin>
                        </Extensibility>";

            fileContent = string.Format(fileContent, Encoding.Default.BodyName, Path.GetDirectoryName(this.GetType().Assembly.Location));
            denaliFilePath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + @"\Microsoft\SQL Server Management Studio\11.0\Addins";
            Directory.CreateDirectory(denaliFilePath);

            using (StreamWriter sw = new StreamWriter(denaliFilePath + "\\Projky.ForDenali.AddIn", false)) {
                sw.Write(fileContent);
            }
        }

        void RemoveDenaliAddinFile() {
            string denaliFilePath;

            denaliFilePath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) +
                             @"\Microsoft\SQL Server Management Studio\11.0\Addins" +
                             "\\Projky.ForDenali.AddIn";
            if (File.Exists(denaliFilePath)) {
                File.Delete(denaliFilePath);
            }
        }

        void SetupHekatonAddinFile() {
            string fileContent;
            string hekatonFilePath;

            fileContent = @"<?xml version=""1.0"" encoding=""{0}"" standalone=""no""?>
                        <Extensibility xmlns=""http://schemas.microsoft.com/AutomationExtensibility"">
	                        <HostApplication>
		                        <Name>Microsoft SQL Server Management Studio</Name>
		                        <Version>*</Version>
	                        </HostApplication>
	                        <Addin>
		                        <FriendlyName>ProjkyAddin</FriendlyName>
		                        <Description>ProjkyAddin for sql 2014</Description>
		                        <Assembly>{1}\Projky.ForHekaton.dll</Assembly>
		                        <FullClassName>Projky.ForHekaton.Connect</FullClassName>
		                        <LoadBehavior>1</LoadBehavior>
		                        <CommandPreload>1</CommandPreload>
		                        <CommandLineSafe>0</CommandLineSafe>
	                        </Addin>
                        </Extensibility>";

            fileContent = string.Format(fileContent, Encoding.Default.BodyName, Path.GetDirectoryName(this.GetType().Assembly.Location));
            hekatonFilePath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + @"\Microsoft\SQL Server Management Studio\12.0\Addins";
            Directory.CreateDirectory(hekatonFilePath);

            using (StreamWriter sw = new StreamWriter(hekatonFilePath + "\\Projky.ForHekaton.AddIn", false)) {
                sw.Write(fileContent);
            }
        }

        void RemoveHekatonAddinFile() {
            string hekatonFilePath;

            hekatonFilePath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + 
                             @"\Microsoft\SQL Server Management Studio\12.0\Addins" +
                             "\\Projky.ForHekaton.AddIn";
            if (File.Exists(hekatonFilePath)) {
                File.Delete(hekatonFilePath);
            }
        }
    }
}
